package com.chancingpack.service.impl;

import com.chancingpack.dal.dao.MyProductDao;
import com.chancingpack.dal.model.Sgbasprodpicture;
import com.chancingpack.service.ImageService;
import com.chancingpack.util.ImageLoadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2017/7/20.
 */
@Service
public class ImageServiceImpl implements ImageService {

    @Autowired
    private MyProductDao myProductDao;

    /**
     * 图片上传到项目路径下
     * @param request
     * @return
     */
    public Map<String,Object> imageLoad(HttpServletRequest request,String status) {
        long start = System.currentTimeMillis();
        Map<String,Object> map = new HashMap<String, Object>();
        ImageLoadUtil imageLoadUtil = new ImageLoadUtil();
        List<Sgbasprodpicture> pictureList = imageLoadUtil.getSgbasprodpictureObject();
        List<String> archIdList = new ArrayList<String>();
        if (pictureList != null) {
            for(Sgbasprodpicture sgbasprodpicture : pictureList){
                archIdList.add(sgbasprodpicture.getArchId());
            }
        }
        //将图片上传到项目本地路径

        map = imageLoadUtil.readImgFromDb(archIdList,request,status);
        String msg = (String)map.get("msg");
        if(msg.contains("缓存错误") || msg.contains("缓存出现异常")){
            status = "0";
            map = imageLoadUtil.readImgFromDb(archIdList,request,status);
        }
//      System.out.println("end" + (System.currentTimeMillis() - start));
        return map;
    }


}
